const listeners = new Map()

export const eventBus = {
  on(event, handler){
    const arr = listeners.get(event) || []
    arr.push(handler)
    listeners.set(event, arr)
  },
  off(event, handler){
    if(!listeners.has(event)) return
    if(!handler){ listeners.delete(event); return }
    listeners.set(event, (listeners.get(event)||[]).filter(h=>h!==handler))
  },
  emit(event, payload){
    ;(listeners.get(event)||[]).forEach(h=>{ try{ h(payload) }catch(e){} })
  }
}



